﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace CoCaNgua
{
    public partial class Form1 : Form
    {
        private string _imgFileName = @"banco.png";
        private string _imgDice = @"dice.png";
        public static Game _g;
        /*
        private Game _g;

        internal Game G
        {
            get { return _g; }
            set { _g = value; }
        }
        /**/
        public string ImgDice
        {
            get { return _imgDice; }
            set { _imgDice = value; }
        }

        public string ImgFileName
        {
            get { return _imgFileName; }
            set { _imgFileName = value; }
        }
        private Image _board;

        public Image Board
        {
            get { return _board; }
            set { _board = value; }
        }

        public Form1()
        {
            InitializeComponent();
            //Board = new Bitmap("");
            Board = LoadImageFromFile(ImgFileName);
            Image dice = LoadImageFromFile(ImgDice);
            _g = new Game(dice, 4);
            foreach (Player p in _g.Players)
            {
                foreach (Knight k in p.Knights)
                {
                    this.Controls.Add(k);
                }
            }
            this.Controls.Add(_g.Players[0].Knights[0]);
            this.Controls.Add(_g.MyDice);
            lbWarning.Text = "Green go...";
        }
        
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == System.Windows.Forms.Keys.Up))
            {
                // Up
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Down))
            {
                // Down
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Left))
            {
                // Left
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Right))
            {
                // Right
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
            {
                // Enter
            }

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            _g.NewGame();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            if (Board!=null)
            e.Graphics.DrawImage(Board, 0, 0);
        }

        private Bitmap LoadImageFromFile(string fname)
        {
            try
            {
                String CodePath =
                    Assembly.GetExecutingAssembly().GetName().CodeBase;

                CodePath = CodePath.Substring(0, CodePath.LastIndexOf(@"\"));

                fname =
                    CodePath + @"\" + fname;

                return new Bitmap(fname);
            }
            catch (System.IO.IOException)
            {
                return null;
            }            
        }

        public void GanThongBao(String str)
        {
            lbWarning.Text = str;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach(Player p in _g.Players){
                if (p.bTurn)
                {
                    p.Knights[0].DatLuot(p.Number);
                }
            }
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}